package com.xls.commonlibrary.util;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.czhj.sdk.common.utils.AppPackageUtil;

import static com.xls.commonlibrary.constant.Const.MMKV_GAME_TYPE;
import static com.xls.commonlibrary.constant.Const.MMKV_GAME_TYPE_GUANHUAI;
import static com.xls.commonlibrary.constant.Const.MMKV_TOKEN;
import static com.xls.commonlibrary.constant.Const.MMKV_TOKEN_LIANSHAN_RISK;

public class TokenUtil {
    private static volatile TokenUtil singleton = null;

    private TokenUtil() {
    }

    public static TokenUtil getInstance() {
        if (singleton == null) {
            synchronized (TokenUtil.class) {
                if (singleton == null) {
                    singleton = new TokenUtil();
                }
            }
        }
        return singleton;
    }

    // token get & set
    // todo 退出登录需要清空缓存
    public void saveToken(String token) {
        MMKVUtil.getInstance().getKV().encode(MMKV_TOKEN, token);
    }

    public String getToken() {
        String token = MMKVUtil.getInstance().getKV().decodeString(MMKV_TOKEN);
        if (token == null){
            token = "";
        }
        return token;
    }

    public void saveGameType(String type, Activity activity) {
//        项目配置关怀版链接才存储关怀版开关
            try {
                if (!TextUtils.isEmpty(AppUtils.getGuanHuaiURL(activity))){
                    MMKVUtil.getInstance().getKV().encode(MMKV_GAME_TYPE, type);
                }else {
                    MMKVUtil.getInstance().getKV().encode(MMKV_GAME_TYPE, "");
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
    }

    public Boolean getGameType() {
        String type = MMKVUtil.getInstance().getKV().decodeString(MMKV_GAME_TYPE);
        if (type == null){
            type = "";
        }
        if (TextUtils.equals(type,MMKV_GAME_TYPE_GUANHUAI)){
            return true;
        }
        return false;
    }

    // 连山风控token
    public void saveTokenLianShanRisk(String token) {
        MMKVUtil.getInstance().getKV().encode(MMKV_TOKEN_LIANSHAN_RISK, token);
    }
    public String getTokenLianShanRisk() {
        String token = MMKVUtil.getInstance().getKV().decodeString(MMKV_TOKEN_LIANSHAN_RISK);
        if (token == null){
            token = "";
        }
        return token;
    }
}
